<?php

/**
 * report actions.
 *
 * @package    sf_sandbox
 * @subpackage report
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class reportActions extends sfActions
{
    public function executeConvertEcashToEpoint()
    {
    }
    public function executeEpointTransfer()
    {
    }
    public function executeGroupSales()
    {
        $dateFromStr = "2013-04-01 00:00:00";
        $dateToStr = "2013-05-15 23:59:59";
        $dateToStr = "2013-05-20 23:59:59";

        //$c = new Criteria();
        //$dists = MlmDistributorPeer::doSelect($c);

        $query = "SELECT newDist.upline_dist_id, dist.distributor_code, SUM(package.price) AS _DS_SALES, dist.tree_structure
        , dist.full_name, dist.email, dist.contact
            FROM mlm_distributor newDist
                LEFT JOIN mlm_package package ON package.package_id = newDist.init_rank_id
                LEFT JOIN mlm_distributor dist ON dist.distributor_id = newDist.upline_dist_id
            where newDist.created_on >= '".$dateFromStr."'
            and newDist.created_on <= '".$dateToStr."' group by newDist.upline_dist_id Having SUM(package.price) >= 20000";

        $query = "SELECT newDist.upline_dist_id, dist.distributor_code, SUM(package.price) AS _DS_SALES, dist.tree_structure
        , dist.full_name, dist.email, dist.contact
            FROM mlm_distributor newDist
                LEFT JOIN mlm_package package ON package.package_id = newDist.init_rank_id
                LEFT JOIN mlm_distributor dist ON dist.distributor_id = newDist.upline_dist_id
            where newDist.placement_tree_structure like '%|768|%'
            and newDist.created_on <= '".$dateToStr."' group by newDist.upline_dist_id Having SUM(package.price) >= 20000";

        $connection = Propel::getConnection();
        $statement = $connection->prepareStatement($query);
        $resultset2 = $statement->executeQuery();

        $resultArray = array();
        $idx = 0;
        while ($resultset2->next()) {
            $arrDist = $resultset2->getRow();

            $distId = $arrDist['upline_dist_id'];
            $dsSales = $arrDist['_DS_SALES'];

            $dateFromStr = "2013-04-01 00:00:00";
            $dateToStr = "2013-05-15 23:59:59";

            $query = "SELECT SUM(package.price) AS _SUM
                    FROM mlm_distributor dist
                        LEFT JOIN mlm_package package ON package.package_id = dist.rank_id
                WHERE dist.placement_tree_structure like '%|".$distId."|%'
                 AND dist.created_on >= '" . $dateFromStr . "'
                 AND dist.created_on <= '" . $dateToStr . "'";

            $connection = Propel::getConnection();
            $statement = $connection->prepareStatement($query);
            $resultset = $statement->executeQuery();

            $totalSales = 0;
            if ($resultset->next()) {
                $arr = $resultset->getRow();
                if ($arr["_SUM"] != null) {
                    $totalSales = $arr["_SUM"];
                }
            }

            $resultSales = $totalSales - $dsSales;

            if ($resultSales < 100000)
                continue;

            $resultArray[$idx]["dist_id"] = $arrDist['upline_dist_id'];
            $resultArray[$idx]["dist_code"] = $arrDist['distributor_code'];
            $resultArray[$idx]["full_name"] = $arrDist['full_name'];
            $resultArray[$idx]["email"] = $arrDist['email'];
            $resultArray[$idx]["contact"] = $arrDist['contact'];
            $resultArray[$idx]["dsSales"] = $dsSales;
            $resultArray[$idx]["total_sales"] = $totalSales;
            $resultArray[$idx]["resultSales"] = $resultSales;

            $idx++;
        }

        $this->resultArray = $resultArray;
    }
    public function executeIndividualTraderSales()
    {
    }
    public function executeMt4Withdrawal()
    {
    }
    public function executeReferralBonus()
    {
    }
    public function executeTotalMt4Reload()
    {
    }
    public function executeTotalPackagePurchase()
    {
    }
    public function executeTotalPackageUpgrade()
    {
    }
    public function executeTotalVolumeTraded()
    {
    }
}
